package com.nsgf.library.ui.page.manager;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.response.DataResult;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityNsgfManagerExamineBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.event.EventMsg;
import com.nsgf.library.event.RxBus;
import com.nsgf.library.ui.adapter.XFragmentAdapter;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.MessageActivity;
import com.nsgf.library.utils.PublicMethodUtils;
import com.nsgf.library.utils.StringUtil;
import com.nsgf.library.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单管理已办审核
 */
public class WorkOrderExamineAlreadyActivity extends BaseActivity {

    MessageActivity.MessageStates states;

    private List<Fragment> fragmentList = new ArrayList<>();

    private String[] mPageTitleList;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {

        states = getActivityScopeViewModel(MessageActivity.MessageStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    private int isResponsibleBy;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.activity_nsgf_manager_examine, BR.vm, states)
                .addBindingParam(BR.click, new ClickProxy());
    }

    MessageAbnormalFragment abnormalFragment;
    MessageAbnormalOpinionFragment opinionFragment;
    private ActivityNsgfManagerExamineBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = (ActivityNsgfManagerExamineBinding) getBinding();
        getLifecycle().addObserver(mAccountRequester);
        states.titleBar.set("巡检");

        isResponsibleBy = getIntent().getIntExtra("isResponsibleBy", 0);

        mAccountRequester.patrolDetail(getIntent().getStringExtra("orderCode"));

        mAccountRequester.getWorkOrderDetail().observe(this, dataResult -> {

            if (dataResult.getResponseStatus().isSuccess()) {
                if (dataResult.getResult().getCode() == 200) {

                    if (StringUtil.isEmpty(dataResult.getResult().getData().getEffect())) {
                        mBinding.btnLogin.setVisibility(View.VISIBLE);
                    } else mBinding.btnLogin.setVisibility(View.GONE);
                    mBinding.viewPager.setOffscreenPageLimit(2);
                    if (dataResult.getResult().getData().getAbnormalList().size() > 0) {
                        abnormalFragment = MessageAbnormalFragment.newInstance(dataResult.getResult().getData().getEffect(), dataResult.getResult().getData().getAbnormalList());
                        fragmentList.add(abnormalFragment);
                        mPageTitleList = new String[]{"异常问题", "审批意见"};
                    } else {
                        fragmentList.add(WorkOrderExamineAlreadyRecordFragment.newInstance(dataResult.getResult().getData()));
                        mPageTitleList = new String[]{"巡查记录", "审批意见"};
                    }
                    opinionFragment = MessageAbnormalOpinionFragment.newInstance(isResponsibleBy, dataResult.getResult().getData());
                    fragmentList.add(opinionFragment);
                    XFragmentAdapter adapter = new XFragmentAdapter(this, getSupportFragmentManager(), fragmentList, mPageTitleList);
                    mBinding.viewPager.setAdapter(adapter);
                    PublicMethodUtils.initMagicIndicator(mBinding.magicIndicator, mBinding.viewPager, mPageTitleList, this);

                } else {
                    if (401 == dataResult.getResult().getCode()) {
                        UserInfo.getInstance().clearUserInfo();
                        finish();
                    }
                    ToastUtils.showLongToast(dataResult.getResult().getMsg());
                }
            } else {
                ToastUtils.showLongToast(dataResult.getResponseStatus().getMsg());
            }
        });

        mAccountRequester.getSrc().observe(this, new Observer<DataResult<ResponseStatus<String>>>() {
            @Override
            public void onChanged(DataResult<ResponseStatus<String>> responseStatusDataResult) {
                if (responseStatusDataResult.getResponseStatus().isSuccess()) {

                    ToastUtils.showLongToast("提交成功");

                    EventMsg eventMsg = new EventMsg();
                    eventMsg.setMsg("warning");
                    RxBus.get().post(eventMsg);
                    EventBus.getDefault().post(eventMsg);
                    finish();

                } else {
                    ToastUtils.showLongToast("请稍后再试...");
                }
            }
        });


    }


    public class ClickProxy {

        public void login() {

            if (StringUtil.isEmpty(opinionFragment.getmBinding().etOpinionConfirmOpinions.getText().toString())
                    || StringUtil.isEmpty(opinionFragment.getmBinding().tvAbnormalEffect.getText().toString())) {

                ToastUtils.showLongToast("请完善审批意见");
                return;
            }
            JsonObject jsonObject = new JsonObject();

            jsonObject.addProperty("confirmOpinions", opinionFragment.getmBinding().etOpinionConfirmOpinions.getText().toString());
            jsonObject.addProperty("effect", StringUtil.isEmpty(opinionFragment.getmBinding().tvAbnormalEffect.getText().toString())
                    ? "很好" : opinionFragment.getmBinding().tvAbnormalEffect.getText().toString());
            if (null != abnormalFragment) {
                JsonArray jsonArray = new JsonArray();
                for (WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal abnormal : abnormalFragment.getAbnormalList()) {

                    if (null == abnormal.getSelectAbnormal()) {
                        ToastUtils.showLongToast("全部审核完成后再提交");
                        return;
                    }
                    JsonObject params = new JsonObject();
                    params.addProperty("id", abnormal.getId());
                    params.addProperty("isAbnormal", abnormal.getSelectAbnormal());
                    params.addProperty("areaCode", abnormal.getAreaCode());
                    params.addProperty("areaName", abnormal.getAreaName());
                    params.addProperty("itemCode", abnormal.getItemCode());
                    params.addProperty("itemName", abnormal.getItemName());
                    params.addProperty("liveImage", abnormal.getLiveImage());
                    params.addProperty("positionCode", abnormal.getPositionCode());
                    params.addProperty("positionName", abnormal.getPositionName());
                    params.addProperty("resultType", abnormal.getResultType());
                    params.addProperty("resultValue", abnormal.getResultValue());
                    params.addProperty("abnormalDescribe", abnormal.getAbnormalDescribe());
                    jsonArray.add(params);
                    jsonObject.addProperty("orderCode", abnormal.getOrderCode());
                }
                jsonObject.add("abnormalList", jsonArray);
            }

            mAccountRequester.reviewWorkOrder(jsonObject);


        }

    }

}
